Option Strict Off
Option Explicit On
Module DisplayFromMemory
	'This function displays a picture in a window from the supplied byte array as opposed to loading it from file.
	
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Integer, ByVal fDeleteOnRelease As Integer, ByRef ppstm As Object) As Integer
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    Private Declare Function OleLoadPicture Lib "olepro32" (ByRef pStream As Object, ByVal lSize As Integer, ByVal fRunmode As Integer, ByRef riid As Object, ByRef ppvObj As Object) As Integer
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Object, ByRef pclsid As Object) As Integer
	Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Integer, ByVal dwBytes As Integer) As Integer
	Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Integer) As Integer
	Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Integer) As Integer
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef pDest As Object, ByRef pSource As Object, ByVal dwLength As Integer)
	
	Public Function PictureFromByteStream(ByRef b() As Byte) As System.Drawing.Image
		Dim LowerBound As Integer
		Dim ByteCount As Integer
		Dim hMem As Integer
		Dim lpMem As Integer
		Dim IID_IPicture(15) As Object
        Dim istm As Object
		
		On Error GoTo Err_Init
		If UBound(b, 1) < 0 Then
			Exit Function
		End If
		
		LowerBound = LBound(b)
		ByteCount = (UBound(b) - LowerBound) + 1
		hMem = GlobalAlloc(&H2, ByteCount)
		If hMem <> 0 Then
			lpMem = GlobalLock(hMem)
			If lpMem <> 0 Then
				MoveMemory(lpMem, b(LowerBound), ByteCount)
				Call GlobalUnlock(hMem)
				'UPGRADE_WARNING: Couldn't resolve default property of object istm. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				If CreateStreamOnHGlobal(hMem, 1, istm) = 0 Then
					'UPGRADE_WARNING: Couldn't resolve default property of object IID_IPicture(0). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					'UPGRADE_ISSUE: StrPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
                    If CLSIDFromString(VarPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}"), IID_IPicture(0)) = 0 Then
                        'UPGRADE_WARNING: Couldn't resolve default property of object PictureFromByteStream. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        'UPGRADE_WARNING: Couldn't resolve default property of object IID_IPicture(0). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
                        Call OleLoadPicture(VarPtr(istm), ByteCount, 0, IID_IPicture(0), PictureFromByteStream)
                    End If
				End If
			End If
		End If
		
		Exit Function
		
Err_Init: 
		If Err.Number = 9 Then
			'Uninitialized array
			Call MsgBox("You must pass a non-empty byte array to this function!", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		Else
			MsgBox(Err.Number & " - " & Err.Description)
			Call MsgBox(Err.Number & " - " & Err.Description, MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
		End If
	End Function
End Module